//
//  NewMessageTableView.m
//  1gene
//
//  Created by 1jiyin on 16/3/15.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//*******************新消息*******************
#import "NewMessageTableView.h"

@implementation NewMessageTableView
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style data:(NSDictionary *)dic
{
    if (self=[super initWithFrame:frame style:style]) {
        self.showsVerticalScrollIndicator=NO;
        self.backgroundColor=[UIColor whiteColor];
        _dataArray=[NSMutableArray arrayWithArray:dic[@"data"]];
        self.delegate=self;
        self.dataSource=self;
        self.tableFooterView=[UIView new];
        self.tableHeaderView=[UIView new];
    }
    return self;
}
//tableView的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    //用户头像
    UIImageView *userHeadImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 56, 56)];
    //userHeadImageView.image=[UIImage imageNamed:@"community-backgroundImage"];
    [userHeadImageView sd_setImageWithURL:[NSURL URLWithString:_dataArray[indexPath.row][@"reply_info"][@"user_info"][@"avatar_url"]] placeholderImage:[UIImage imageNamed:@"noImage_background"]];
     userHeadImageView.layer.cornerRadius=56/2.0;
    userHeadImageView.layer.masksToBounds=YES;
    [cell.contentView addSubview:userHeadImageView];
    
    
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:_dataArray[indexPath.row][@"reply_info"][@"user_info"][@"avatar_url"]]];
    UIImage *myHeadImage=[UIImage imageWithData:data];
    userHeadImageView.image=myHeadImage;
    
    
    
    
    
    //用户姓名
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(userHeadImageView.frame), 56, 20)];
    name.text=_dataArray[indexPath.row][@"reply_info"][@"user_info"][@"nikename"];
    name.font=[UIFont systemFontOfSize:12];
    name.textAlignment=NSTextAlignmentCenter;
    name.textColor=[UIColor colorWithRed:106/255.0 green:198/255.0 blue:223/255.0 alpha:1];
    [cell.contentView addSubview:name];
    
    //帖子标题
    UILabel *topic=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(userHeadImageView.frame)+8, 5+(56-35)/2.0, self.frame.size.width-CGRectGetMaxX(userHeadImageView.frame)-3, 15)];
    topic.text=_dataArray[indexPath.row][@"topic_title"];
    topic.font=[UIFont systemFontOfSize:15];
    topic.textColor=[UIColor colorWithRed:106/255.0 green:198/255.0 blue:223/255.0 alpha:1];
    [cell.contentView addSubview:topic];
    
    //评论的内容
    UILabel *comment=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(userHeadImageView.frame)+8, CGRectGetMaxY(topic.frame), self.frame.size.width-CGRectGetMaxX(userHeadImageView.frame)-3, 25)];
    comment.text=_dataArray[indexPath.row][@"reply_info"][@"content"];
    comment.font=[UIFont systemFontOfSize:13];
    comment.numberOfLines=0;
    comment.textColor=[UIColor colorWithRed:106/255.0 green:198/255.0 blue:223/255.0 alpha:1];
    [cell.contentView addSubview:comment];
    
    
    //划线
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 85.5, self.frame.size.width, 0.5)];
    line.backgroundColor=[UIColor colorWithRed:212/255.0 green:213/255.0 blue:214/255.0 alpha:1];
    [cell.contentView addSubview:line];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 86;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *topicId=[NSString stringWithFormat:@"%@",_dataArray[indexPath.row][@"reply_info"][@"topic_id"]];
    NSString *replyId=[NSString stringWithFormat:@"%@",_dataArray[indexPath.row][@"id"]];
    [_dataArray removeObjectAtIndex:indexPath.row];
    [self reloadData];
    [self.myDelegate gotoXiangQing:topicId];
    [self deleteMessage:replyId];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *replyId=[NSString stringWithFormat:@"%@",_dataArray[indexPath.row][@"id"]];
    [_dataArray removeObjectAtIndex:indexPath.row];
    [self reloadData];
    [self deleteMessage:replyId];
}
//-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return @"删除";
//}

-(void)deleteMessage:(NSString *)replyId
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    [manager DELETE:[NSString stringWithFormat:DeleteTopicMessage,replyId,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"删除帖子消息%@",dic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
